//
//  SCNewfeatureViewController.swift
//  weibo
//
//  Created by 阿城 on 15/12/8.
//  Copyright © 2015年 阿城. All rights reserved.
//

import UIKit
import SnapKit

private let count = 4
private let width = UIScreen.mainScreen().bounds.width
private let height = UIScreen.mainScreen().bounds.height

class SCNewfeatureViewController: UIViewController, UIScrollViewDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()

    }

    private lazy var scroll : UIScrollView = {
        let scr = UIScrollView()
        scr.frame = UIScreen.mainScreen().bounds
        scr.bounces = false
        scr.pagingEnabled = true
        scr.showsHorizontalScrollIndicator = false
        scr.contentSize = CGSize(width: CGFloat(count) * width, height: 0)
        scr.delegate = self
        return scr
    }()
    
    private lazy var pageControl : UIPageControl = {
        let page = UIPageControl()
        page.currentPageIndicatorTintColor = UIColor.blackColor()
        page.pageIndicatorTintColor = UIColor.grayColor()
        page.numberOfPages = count
        return page
    }()
    

    private func setupUI(){
        
        view.addSubview(scroll)
        view.addSubview(pageControl)
        
        for i in 0..<count {
            
            let imageView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            imageView.frame = CGRect(x: CGFloat(i) * width, y: 0, width: width, height: height)
            scroll.addSubview(imageView)
            
            if i == count - 1{
                
                imageView.userInteractionEnabled = true
                imageView.addSubview(comeInBtn)
                imageView.addSubview(shareBtn)
                
            }
        }
        
        
        pageControl.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.bottom.equalTo(view.snp_bottom).offset(-50)
        }
        
        comeInBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(comeInBtn.superview!)
            make.bottom.equalTo(view.snp_bottom).offset(-150)
        }
        
        shareBtn.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(comeInBtn)
            make.bottom.equalTo(comeInBtn.snp_top).offset(-20)
        }
        
        

    }
    
    // MARK: - 进入按钮
    private lazy var comeInBtn: UIButton = {
        
        let comeIn = UIButton()
        comeIn.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        comeIn.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        comeIn.setTitle("立即进入", forState: UIControlState.Normal)
        comeIn.sizeToFit()
        comeIn.addTarget(self, action: "comeInClick", forControlEvents: UIControlEvents.TouchUpInside)
        return comeIn
    }()
    
    @objc private func comeInClick(){
        
        NSNotificationCenter.defaultCenter().postNotificationName(kSwitchVcNotifcation, object: self)
        
    }
    
    
    // MARK: - 分享按钮
    private lazy var shareBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        btn.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Selected)
        btn.setTitle("  分享微博", forState: UIControlState.Normal)
        btn.setTitleColor(UIColor.darkGrayColor(), forState: UIControlState.Normal)
        btn.titleLabel?.font = UIFont.systemFontOfSize(16)
        btn.sizeToFit()
        btn.addTarget(self, action: "shareClick:", forControlEvents: UIControlEvents.TouchUpInside)
        return btn
    }()
    
    @objc private func shareClick(sender: UIButton){
        sender.selected = !sender.selected
    }
    
    
    // MARK: - delegate
    
    func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        
        let num = scroll.contentOffset.x / width
        pageControl.currentPage = Int( num )

    }

}
